package com.fileupload.upload.log.strategy;

import com.fileupload.config.ApplicationConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 日志存储策略工厂
 * 根据配置的logType获取对应的存储策略实现
 */
@Component
public class LogStorageStrategyFactory {
    
    private static final Logger logger = LoggerFactory.getLogger(LogStorageStrategyFactory.class);
    
    // 策略实现缓存
    private final Map<String, LogStorageStrategy> strategyMap = new HashMap<>();
    
    @Autowired
    private ApplicationConfig applicationConfig;
    
    @Autowired
    @Qualifier("mySqlLogStorageStrategy")
    private LogStorageStrategy mysqlStrategy;
    
    @Autowired
    @Qualifier("redisLogStorageStrategy")
    private LogStorageStrategy redisStrategy;
    
    /**
     * 初始化策略映射
     */
    @PostConstruct
    public void init() {
        // 注册所有的策略实现
        registerStrategy(mysqlStrategy);
        registerStrategy(redisStrategy);
        
        logger.info("[日志策略工厂] 初始化完成，已注册的策略: {}", strategyMap.keySet());
    }
    
    /**
     * 注册策略实现
     * @param strategy 策略实现
     */
    private void registerStrategy(LogStorageStrategy strategy) {
        if (strategy != null) {
            String type = strategy.getType();
            strategyMap.put(type, strategy);
            logger.info("[日志策略工厂] 注册策略: {}", type);
        }
    }
    
    /**
     * 获取当前配置的日志存储策略
     * @return 日志存储策略实现
     */
    public LogStorageStrategy getCurrentStrategy() {
        String logType = "mysql"; // 默认使用MySQL策略
        if (applicationConfig != null && applicationConfig.getLog() != null) {
            logType = applicationConfig.getLog().getLogType();
        }
        if (logType == null || logType.isEmpty()) {
            logType = "mysql"; // 默认使用MySQL策略
        }
        return getStrategyByType(logType);
    }
    
    /**
     * 根据类型获取指定的策略实现
     * @param type 策略类型
     * @return 日志存储策略实现
     */
    public LogStorageStrategy getStrategyByType(String type) {
        if (type == null || type.trim().isEmpty()) {
            logger.warn("[日志策略工厂] 策略类型为空，返回默认MySQL策略");
            return mysqlStrategy; // 默认使用MySQL策略
        }
        
        LogStorageStrategy strategy = strategyMap.get(type.toLowerCase());
        if (strategy == null) {
            logger.warn("[日志策略工厂] 未找到策略类型: {}, 返回默认MySQL策略", type);
            return mysqlStrategy; // 未找到时返回默认MySQL策略
        }
        
        logger.debug("[日志策略工厂] 获取策略: {}", type);
        return strategy;
    }
    
    /**
     * 检查策略类型是否支持
     * @param type 策略类型
     * @return 是否支持
     */
    public boolean isStrategySupported(String type) {
        return type != null && strategyMap.containsKey(type.toLowerCase());
    }
    
    /**
     * 获取所有支持的策略类型
     * @return 支持的策略类型集合
     */
    public java.util.Set<String> getAllSupportedTypes() {
        return new java.util.HashSet<>(strategyMap.keySet());
    }
}